package br.com.mayco.persistence;

import java.io.Serializable;
import java.lang.reflect.Method;

/**
 * @author Mayco Lucian
 * @since 25/02/2013
 * version 1.0.0
 */

public class EntityDecorator implements Serializable {

    private Object entity;

    public EntityDecorator(Object entity) {
        this.entity = entity;
    }

    public Object getPrimaryKey() {
        try {
            Method[] methods = entity.getClass().getMethods();
            for (Method m : methods) {
                if (m.getName().equalsIgnoreCase("getId")) {
                    return m.invoke(entity);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

//        try {
//            Field[] declaredFields = entity.getClass().getDeclaredFields();
//            Field[] fields = entity.getClass().getFields();
//            Field[] ff = new Field[declaredFields.length + fields.length];
//            System.arraycopy(declaredFields, 0, ff, 0, declaredFields.length);
//            System.arraycopy(fields, 0, ff, declaredFields.length, fields.length);
//
//            for (Field f : ff) {
//                Id id = f.getAnnotation(Id.class);
//                if (id == null) {
//                    continue;
//                }
//                return f.get(entity);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
    }

    public String getSelect() {
        return entity.getClass().getSimpleName() + ".findAll";
    }
}
